package com.jcl.dd.datasource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Component
public class DynamicDataSource extends AbstractRoutingDataSource {


    public DynamicDataSource(LoadDataSource loadDataSource) {
        Map<String, DataSource> map = loadDataSource.loadAllDataSource();
        super.setTargetDataSources(new HashMap<>(map));
        super.setDefaultTargetDataSource(map.get(DataSourceType.DEFAULT_DS_NAME));
        super.afterPropertiesSet();
    }

    /**
     * @return 返回数据源名称
     */
    @Override
    protected Object determineCurrentLookupKey() {
        return DynamicDataSourceHolder.getDataSourceType();
    }
}
